﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Services
{
	using System;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Repositories;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Extensions;

	public class PollService : IPollService
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="PollService"/> class.
		/// </summary>
		/// <param name="identityService">The identity service.</param>
		/// <param name="pollChoiceService">The poll choice service.</param>
		/// <param name="pollTrackingService">The poll tracking service.</param>
		/// <param name="pollRepository">The poll repository.</param>
		public PollService(
			IIdentityService identityService, 
			IPollChoiceService pollChoiceService, 
			IPollTrackingService pollTrackingService,
			IPollRepository pollRepository)
		{
			this.IdentityService = identityService;
			this.PollChoiceService = pollChoiceService;
			this.PollTrackingService = pollTrackingService;
			this.PollRepository = pollRepository;
		}

		/// <summary>
		/// Gets or sets the identity service.
		/// </summary>
		/// <value>The identity service.</value>
		public IIdentityService IdentityService
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets or sets the poll choice service.
		/// </summary>
		/// <value>The poll choice service.</value>
		public IPollChoiceService PollChoiceService
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets or sets the poll tracking service.
		/// </summary>
		/// <value>The poll tracking service.</value>
		public IPollTrackingService PollTrackingService
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets or sets the poll repository.
		/// </summary>
		/// <value>The poll repository.</value>
		public IPollRepository PollRepository
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets the poll by id.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <returns></returns>
		public Poll GetById(int id)
		{
			var poll = this.PollRepository.GetById(id);

			poll.Choices = this.PollChoiceService.GetAll(new PollChoiceSpecification
			{
				PollId = poll.Id
			});

			var identity = IdentityService.Current;

			if (identity != null)
			{
				var tracking = this.PollTrackingService.GetBy(new PollTrackingSpecification
				{
					PollId = poll.Id,
					UserId = identity.Id
				});

				poll.HasVoted = (tracking != null);
				poll.CanVote = !poll.HasVoted;
			}
			else
			{
				poll.HasVoted = false;
				poll.CanVote = false;
			}

			return poll;
		}

		/// <summary>
		/// Creates the poll.
		/// </summary>
		/// <returns></returns>
		public Poll Create()
		{
			var poll = new Poll
			{
				Created = DateTime.Now
			};

			poll.Updated = poll.Created;

			return poll;
		}

		/// <summary>
		/// Inserts the specified poll.
		/// </summary>
		/// <param name="poll">The poll.</param>
		public void Insert(Poll poll)
		{
			poll.Validate();

			poll.UserId = IdentityService.Current.Id;
			poll.Slug = poll.Title.Slug();
			poll.Start = poll.Created;
			poll.Expires = DateTime.MaxValue;

			this.PollRepository.Insert(poll);
		}

		/// <summary>
		/// Updates the specified poll.
		/// </summary>
		/// <param name="poll">The poll.</param>
		public void Update(Poll poll)
		{
			poll.Validate();

			poll.Slug = poll.Title.Slug();
			poll.Updated = DateTime.Now;

			this.PollRepository.Update(poll);
		}

		/// <summary>
		/// Deletes the specified poll.
		/// </summary>
		/// <param name="poll">The poll.</param>
		public void Delete(Poll poll)
		{
			this.PollRepository.Delete(poll);
		}
	}
}